from rest_framework import serializers
from six_it.models import Label,Question,Article,User
class ArticleSerializersMin(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ['id', 'title']
class UserSerializers(serializers.ModelSerializer):
    fans = serializers.PrimaryKeyRelatedField(many=True,read_only=True)
    articles = ArticleSerializersMin(many=True,read_only=True)
    class Meta:
        model = User
        fields = ['id','username','avatar','articles','fans']

class ArticleSerializers(serializers.ModelSerializer):
    collected_users = serializers.PrimaryKeyRelatedField(many=True,read_only=True)
    user = UserSerializers(read_only=True)
    collected = serializers.BooleanField(default=False)
    class Meta:
        model = Article
        fields = ['id','title','content','createtime','user','collected','image','visits','collected_users']
class QuestionsSerializers(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)

    labels = serializers.StringRelatedField(read_only=True,many=True)
    class Meta:
        model = Question
        fields = ['id','title','labels','user','createtime','reply',
                  'replyname','replytime','unuseful_count','useful_count'
            ,'visits',]
class LabelRRetrieveSerializers(serializers.ModelSerializer):
    # user = serializers.PrimaryKeyRelatedField(many=True,read_only=True)
    # id = serializers.IntegerField(read_only=True)
    questions = QuestionsSerializers(many=True)
    articles = ArticleSerializers(many=True)
    class Meta:
        model = Label
        fields = ['id','questions','label_name','desc','baike_url',
                  'label_icon','users','articles']